package com.example.basic;

import java.util.concurrent.*;

/**
 * @author zhangs
 * @version v1.0
 * @decription 线程创建的方式
 * @date 2021/3/28 11:22
 **/
public class ThreadCreate {
    /**
     * 1.继承Thread类
     */
    static class Task extends Thread {
        @Override
        public void run() {
            System.out.println("任务执行！");
        }
    }

    /**
     * 2.实现Runnable接口
     */
    static class Run implements Runnable {
        @Override
        public void run() {
            System.out.println("任务执行！");
        }
    }

    /**
     * 3.实现Callable接口
     */
    static class Call implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("线程执行！");
            return 10;
        }
    }

    public static void main(String[] args) {
        // 1.继承Thread类
        new Task().start();

        // 2.继承Runnable类
        new Thread(new Run()).start();

        // 3.实现Callable接口
        ExecutorService service = Executors.newFixedThreadPool(10);
        Future<Integer> future = service.submit(new Call());
        try {
            Integer integer = future.get();
            System.out.println(integer);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            service.shutdownNow();
        }
    }
}
